package com.wdcg.utils;

import java.util.ArrayList;
import java.util.List;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationClient.OnAddGeofencesResultListener;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.wdcg.model.access.DwarfAccess;
import com.wdcg.model.dao.Dwarf;
import com.wdcg.model.dao.DwarfDescription;
import com.wdcg.model.dbhelper.DatabaseHelper;
import com.wdcg.service.ReceiveTransitionsIntentService;

public class LocationProcessor implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener,LocationListener, OnAddGeofencesResultListener {

	private LocationClient locationClient;
	private LocationRequest locationRequest;
	
	private LocationProcessorUpdate listener;
	
	private final Context mContext;
	
	private List<Geofence> listOfGeofences;
	
	public LocationProcessor(Context context, LocationProcessorUpdate listener){
		
		this.mContext = context;
		this.listener = listener;
		
		if(isGooglePlayServicesAvailable()){
		   locationClient = new LocationClient(context, this, this);
		   locationClient.connect();
		}
		else{
		   Toast.makeText(context, "Google Play Service not installed", Toast.LENGTH_LONG).show();
		}
		
        
        

	}
	
	private boolean isGooglePlayServicesAvailable(){
		int resp = GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
		
		return resp == ConnectionResult.SUCCESS;
	}
	
	public void onConnectionFailed(ConnectionResult arg0) {
		// TODO Auto-generated method stub
		
	}

	public void onConnected(Bundle arg0) {
		
		locationRequest = LocationRequest.create();
		locationRequest.setInterval(5000);
		locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
		locationClient.requestLocationUpdates(locationRequest, this);
		
		createGeofences();
		
	}

	public void onDisconnected() {
		// TODO Auto-generated method stub
		
	}

	public void onLocationChanged(Location location) {
		listener.onLocationChanged(location);
		
	}
	
	public void connectLocationClient() {
		if (!locationClient.isConnected() && isGooglePlayServicesAvailable()) {
			locationClient.connect();
		}
	}
	
	public void disconnectLocationClient() {
		if (locationClient.isConnected()) {
			locationClient.disconnect();
		}
	}
	
	public void createGeofences(){
		
		
		listOfGeofences =  new ArrayList<Geofence>();
		
		List<DwarfDescription> listOfDescriptions = DwarfAccess.getLockedAndInvisibleDwarfsDescription();
		
		for(DwarfDescription desc: listOfDescriptions){
			Dwarf dwarf = desc.getDwarf();
			
			SimpleGeofence mGeofence = new SimpleGeofence(dwarf.getId()+"", dwarf.getLatitude(), dwarf.getLongitude(), 50.0f, Geofence.NEVER_EXPIRE, Geofence.GEOFENCE_TRANSITION_ENTER);
			listOfGeofences.add(mGeofence.toGeofence());		
		}
		
		locationClient.addGeofences(listOfGeofences, getTransitionPendingIntent(), this);
		
	}
	
	public interface LocationProcessorUpdate {
		public void onLocationChanged(Location location);
	}

	public void onAddGeofencesResult(int statusCode, String[] geofenceRequestIds) {
		
	}
	
	private PendingIntent getTransitionPendingIntent() {
        // Create an explicit Intent
        Intent intent = new Intent(mContext,
        		ReceiveTransitionsIntentService.class);
        /*
         * Return the PendingIntent
         */
        return PendingIntent.getService(
                mContext,
                0,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
    }

}
